package com.ceres.system.controller;

import com.ceres.entity.PageResult;
import com.ceres.entity.Result;
import com.ceres.entity.StatusCode;
import com.ceres.system.pojo.User;
import com.ceres.system.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //private String name = SecurityContextHolder.getContext().getAuthentication().getName();
    private String username="001";

    @GetMapping("/queryUser")
    public Result queryUser(@RequestParam Map<String, Object> queryCondition, @RequestParam(required = false,defaultValue = "1") int page, @RequestParam(required = false,defaultValue = "5") int size){
        try {
            Page<User> pageList = userService.queryUser(queryCondition, page, size,username);
            PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
            return new Result(true, StatusCode.OK,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    @PostMapping("/add")
    public Result addUser(Integer[] roleIds,@RequestBody User user){
        try {
            userService.addUser(roleIds,user,username);
            return new Result(true,StatusCode.OK);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true, StatusCode.ERROR);
        }
    }

    @DeleteMapping("/deleteById/{id}")
    public Result del(@PathVariable(name = "id") Integer id){
        try {
            userService.delete(id);
            return new Result(true,StatusCode.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    @PutMapping("/update")
    public Result update(@RequestBody User user,Integer[] roleIds){
        try {
            userService.updateUser(roleIds, user);
            return new Result(true,StatusCode.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable(name = "id")Integer id){
        try {
            User user = userService.findById(id);
            return new Result(true,StatusCode.OK,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    @GetMapping("/findRoleByUser/{id}")
    public Result findRoleByUser(@PathVariable(name = "id")Integer id){
        try {
            Integer[] roleByUser = userService.findRoleByUser(id);
            return new Result(true,StatusCode.OK,roleByUser);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    @PutMapping("/reset/{id}")
    public Result reset(@PathVariable(name = "id")Integer id){
        try {
            userService.resetPassword(id);
            return new Result(true,StatusCode.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    @PutMapping("/alterPassword")
    public Result alterPassword(String password){
        try {
            userService.changPassword(password,username);
            return new Result(true,StatusCode.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    @GetMapping("/admin")
    public Result queryAdmin(@RequestParam Map queryCondition, @RequestParam(required = false,defaultValue = "1") int page, @RequestParam(required = false,defaultValue = "5") int size){
        try {
            Page<User> pageList = userService.queryAdmin(queryCondition, page, size,username);
            PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
            return new Result(true, StatusCode.OK,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

}
